<?php
    // part of the pages controller. Only used to display pages
    // created in the admin section
    class Content_Controller extends Application {
        
        var $page_to_load = false; // empty request, load webpage
        
        function setup()
        {
            $this->actions = array('index','load_page');
            use_helper('admin/pages');
            use_model('pages','page_content');
        }
        
		function index() 
		{		    		    
		    $pages = new Pages();
		    // get page by id
            $index_name = $pages->get_index_page();
	        if ( empty($index_name) )
	        {
	            $this->assign('msg',"You do not have a page marked as your index page! ".
	                                 link_to('Do that here',
	                                 array('controller'=>'admin/pages' ))
	                         );
	            return;
	        }
		    
	        if ( $pages->load('index_name=?', array(rawurlencode($index_name))) )
	        {
	            // check that status of page is not 0	            
	            if ($pages->get('status') == 0) {
	               $this->error_404();
	            }
	            
	            $this->assign_ref('page',$pages);
	            $this->assign('admin_edit',false);
	            
	            // render page
	            if ( $pages->get('disable_default_layout') )
	            {
	                $this->use_layout(false);
	            } else {
	                $this->use_layout('application.php');
	            }
	            $this->assign('meta_keywords',$pages->get('meta_keywords'));
	            $this->assign('meta_description',$pages->get('meta_description'));
	            $title = $pages->get('meta_title');
	            $title = empty($title) ? $pages->get('title') : $title;
	            $this->assign('meta_page_title',$title);
	            $this->render(array(
	                        'template'=>VIEWS.'/admin/pages/templates/'.$pages->get('template_file'),
	                        'file'=>true
	                        ));
	            
	        } else {
	            $this->error_404();
	        }
		}
		
		function load_page() 
		{		    
		    $this->_inspect_url();
		    $pages = new Pages();		    
	        if ( $this->page_to_load 
	             && $pages->load('index_name=?', 
	                             array(rawurlencode($this->page_to_load))
	                             )
	           )
	        {
	            // check that status of page is not 0
	            if ($pages->get('status') == 0) {
	               $this->error_404();
	            }
	            
	            $this->assign_ref('page',$pages);
	            $this->assign('admin_edit',false);
	            
	            // render page
	            if ( $pages->get('disable_default_layout') )
	            {
	                $this->use_layout(false);
	            } else {
	                $this->use_layout('application.php');
	            }
	            $this->assign('meta_keywords',$pages->get('meta_keywords'));
	            $this->assign('meta_description',$pages->get('meta_description'));
	            $title = $pages->get('meta_title');
	            $title = empty($title) ? $pages->get('title') : $title;
	            $this->assign('meta_page_title',$title);
	            $this->render(array(
	                        'template'=>VIEWS.'/admin/pages/templates/'.$pages->get('template_file'),
	                        'file'=>true
	                        ));
	            
	        } else {
	            $this->error_404();
	        }
		}
		
		function _inspect_url()
		{
		    // quick function check
		    if (strtolower($this->params['page1']) == 'load_page') {
		        return false;
		    }
		    $url_sections = array();
		    for ($i=1; $i < 7; $i++) {
		        if(empty($this->params['page'.$i])) break; 
		        $url_sections[] = $this->params['page'.$i];
		    }
		    $page_count = count($pieces);
		    if ($page_count < 1) {
		        $this->page_to_load = false;
		    }
		    
		    // now we have to validate the url structure, make sure page actually belongs under
		    // the page, like about/joe-smith/his-balls
            $pages = new Pages();
            $this->page_to_load = $pages->validate_url($url_sections);
		}

        
    }

?>